/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.geometricmodel;

import org.openaecfm.model.geometricmodel.SectionedSpine;
import org.openaecfm.model.geometry.Axis2Placement3D;
import org.openaecfm.model.geometry.CompositeCurve;
import org.openaecfm.model.profile.ProfileDef;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class SectionedSpineIO extends
		AbstractEntityInstanceInputImpl<SectionedSpine> implements
		IEntityInstanceOutput<SectionedSpine> {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(SectionedSpine pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCSECTIONEDSPINE",
				appDataManager.getName(pojo));

		// Set SpineCurve
		EntityInstanceNameParameter spine = new EntityInstanceNameParameter(
				appDataManager.getName(pojo.getSpineCurve()));
		ei.addParameter(spine);

		// Set CrossSections
		ListParameter sections = new ListParameter();
		for (ProfileDef profile : pojo.getCrossSections()) {
			EntityInstanceNameParameter n = new EntityInstanceNameParameter(
					appDataManager.getName(profile));
			sections.add(n);
		}
		ei.addParameter(sections);

		// Set CrossSectionPositions
		ListParameter positions = new ListParameter();
		for (Axis2Placement3D axis : pojo.getCrossSectionPositions()) {
			EntityInstanceNameParameter n = new EntityInstanceNameParameter(
					appDataManager.getName(axis));
			positions.add(n);
		}
		ei.addParameter(positions);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected SectionedSpine doCreateObject() {
		return new SectionedSpine();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			SectionedSpine pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set SpineCurve
		EntityInstanceNameParameter spine = (EntityInstanceNameParameter) sei
				.getParameter(0);
		pojo.setSpineCurve((CompositeCurve) appDataManager.getObject(spine
				.toInstanceName()));

		// Set CrossSections
		ListParameter sections = (ListParameter) sei.getParameter(1);
		for (Parameter p : sections) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
			pojo.getCrossSections().add(
					(ProfileDef) appDataManager.getObject(n.toInstanceName()));
		}

		// Set CrossSectionPositions
		ListParameter positions = (ListParameter) sei.getParameter(2);
		for (Parameter p : positions) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
			pojo.getCrossSectionPositions().add(
					(Axis2Placement3D) appDataManager.getObject(n
							.toInstanceName()));
		}
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			SectionedSpine pojo) {
		return;
	}
}
